<?php

class Sistema_Form_Ambientes extends Zend_Form {

    public function init() {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_pdr');

        $id = $front->getRequest()->getParam('id', 0);

        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $el = new Zend_Form_Element_Hidden('AMB_ID', array('disableLoadDefaultDecorators' => true));
        $el->addDecorators(array(array('ViewHelper'), array('Errors')))->addFilter('Int');
        $this->addElement($el);

        $el = new Zend_Form_Element_Text('AMB_NOM');
        $el->setLabel('Nome')->setRequired(true);
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('AMB_TILPAG');
        $el->setLabel('Título com quebra');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Textarea('AMB_DSC');
        $el->setLabel('Breve Descrição');
        $el->setAttrib('style', 'height: 60px;');
        $el->setAttrib('maxlength', '150');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Textarea('AMB_TXT');
        $el->setLabel('Descrição');
        $el->setAttrib('style', 'height: 120px;');
        $this->addElement($el);
        
        $img = glob('public/upload/'.$controller.'/'.$id.'.*');
        if(is_array($img))
        foreach($img as $src) {
            $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>'<img height="200" style="max-width:600px" src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        $el = new Zend_Form_Element_File('AMB_FILE');
        $el->setLabel('Imagem - Resolução: 1920 x 1080');
        $el->addValidator('Extension', false, array('jpg', 'jpeg', 'png', 'gif'));
        $this->addElement($el);
        
        
        $list_for = '0';
        $html = '';
        $htmli = '';
        $arr_set = array();
        $result = array();
        if($id > 0){
            $query2 = 'SELECT SQL_CACHE FOR_ID FROM AMBIENTES_FORNECEDOR WHERE AMB_ID = '.$id;
            $result2 = $db->fetchAll($query2);
            foreach($result2 as $row2){
                $arr_set[$row2['FOR_ID']] = $row2['FOR_ID'];
                $list_for .= ','.$row2['FOR_ID'];
            }
        }
        $htmli = '<select id="FOR_ID" name="FOR_ID[]" class="chosen-select" multiple="multiple" style="width:500px;">';
        $query = 'SELECT SQL_CACHE FOR_ID,FOR_NOM FROM FORNECEDOR WHERE FOR_STA = 1 ORDER BY FOR_NOM ASC';
        $result = $db->fetchAll($query);
        foreach($result as $row){
            $selected = array_search($row['FOR_ID'], $arr_set) > '' ? 'selected=""':'';            
            $htmli .= '<option '.$selected.' value="'.$row['FOR_ID'].'">'.$row['FOR_NOM'].'</option>'; 
        }
        $htmli .= '</select><br><br>';
        $html .= '<div id="cmb_for">'.$htmli.'</div>';
        $el = new Zend_Form_Element('FOR_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Fornecedores');
        $this->addElement($el);
        
        
        $list_lin = '0';
        $html = '';
        $htmli = '';
        $arr_set = array();
        $result = array();
        if($id > 0){
            $query2 = 'SELECT SQL_CACHE LIN_ID FROM AMBIENTES_LINHA WHERE AMB_ID = '.$id;
            $result2 = $db->fetchAll($query2);
            foreach($result2 as $row2) 
            {
                $arr_set[$row2['LIN_ID']] = $row2['LIN_ID'];
                $list_lin .= ','.$row2['LIN_ID'];
            }
            
            $query = 'SELECT SQL_CACHE LIN_ID,LIN_NOM FROM LINHA WHERE FOR_ID in('.$list_for.') AND LIN_STA = 1 ORDER BY LIN_NOM ASC';
            $result = $db->fetchAll($query);
        }
        $htmli = '<select id="LIN_ID" name="LIN_ID[]" class="chosen-select" multiple="multiple" style="width:500px;">';
        foreach($result as $row){
            $selected = array_search($row['LIN_ID'], $arr_set) > '' ? 'selected=""':'';            
            $htmli .= '<option '.$selected.' value="'.$row['LIN_ID'].'">'.$row['LIN_NOM'].'</option>'; 
        }
        $htmli .= '</select><br><br>';
        $html .= '<div id="cmb_linha">'.$htmli.'</div>';
        $el = new Zend_Form_Element('LIN_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Linhas');
        $this->addElement($el);
        
        
        $html = '';
        $htmli = '';
        $arr_set = array();
        $result = array();
        if($id > 0){
            $query2 = 'SELECT SQL_CACHE COR_ID FROM AMBIENTES_CORES WHERE AMB_ID = '.$id;
            $result2 = $db->fetchAll($query2);
            foreach($result2 as $row2)$arr_set[$row2['COR_ID']] = $row2['COR_ID'];

            $query = 'SELECT SQL_CACHE COR_ID,COR_NOM FROM CORES WHERE LIN_ID in('.$list_lin.') AND COR_STA = 1 ORDER BY COR_NOM ASC';
            $result = $db->fetchAll($query);
        }
        $htmli = '<select id="COR_ID" name="COR_ID[]" class="chosen-select" multiple="multiple" style="width:500px;">';
        foreach($result as $row){
            $selected = array_search($row['COR_ID'], $arr_set) > '' ? 'selected=""':'';            
            $htmli .= '<option '.$selected.' value="'.$row['COR_ID'].'">'.$row['COR_NOM'].'</option>'; 
        }
        $htmli .= '</select><br><br>';
        $html .= '<div id="cmb_cores">'.$htmli.'</div>';
        $el = new Zend_Form_Element('COR_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Cores');
        $this->addElement($el);
        
        $html = '';
        $htmli = '';
        $arr_set = array();
        $result = array();
        if($id > 0){
            $query2 = 'SELECT SQL_CACHE ACA_ID FROM AMBIENTES_ACABAMENTOS WHERE AMB_ID = '.$id;
            $result2 = $db->fetchAll($query2);
            foreach($result2 as $row2)$arr_set[$row2['ACA_ID']] = $row2['ACA_ID'];

            $query = 'SELECT SQL_CACHE ACA_ID,ACA_NOM FROM ACABAMENTOS WHERE LIN_ID in('.$list_lin.') AND ACA_STA = 1 ORDER BY ACA_NOM ASC';
            $result = $db->fetchAll($query);
        }
        $htmli = '<select id="ACA_ID" name="ACA_ID[]" class="chosen-select" multiple="multiple" style="width:500px;">';
        foreach($result as $row){
            $selected = array_search($row['ACA_ID'], $arr_set) > '' ? 'selected=""':'';            
            $htmli .= '<option '.$selected.' value="'.$row['ACA_ID'].'">'.$row['ACA_NOM'].'</option>'; 
        }
        $htmli .= '</select><br><br>';
        $html .= '<div id="cmb_aca">'.$htmli.'</div>'; 
        $el = new Zend_Form_Element('ACA_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Acabamentos');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Text('AMB_ORD');
        $el->setLabel('Ordem')->setRequired(true);
        $el->setAttrib('class', 'numero nummin');
        $el->setAttrib('maxlength', '3');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Checkbox('AMB_STA',array('checked'=>'checked'));
        $el->setLabel('Ativo');
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('Salvar', array('disableLoadDefaultDecorators' => true));
        $btn->setLabel('Salvar');
        $btn->addDecorators(array(array('ViewHelper'), array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);

        $btn = new Zend_Form_Element_Button('Cancelar', array('disableLoadDefaultDecorators' => true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="' . $url . '" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
    }

}
